package algorithm.shipinke;

public class QuickSort {
    public static void quickSort(int[] arr, int begin, int end) {
        if (begin > end) return;
        int pivot = parition(arr, begin, end);
        quickSort(arr, begin, pivot-1);
        quickSort(arr, pivot+1, end);
    }

   public static int parition(int[] arr, int begin, int end) {
        int base = arr[begin];

        while (begin < end) {
            while (begin < end && arr[end] >= base) {
                end --;
            }
            arr[begin] = arr[end];

            while (begin < end && arr[begin] <= base) {
                begin ++;
            }
            arr[end] = arr[begin];
        }
        arr[begin] = base;
        return begin;
   }

    public static void main(String[] args) {
        int[] arr = {15, 9, 35, 44, 56, 72, 2};
        quickSort(arr, 0, arr.length-1);
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }
}
